package com.xinqi.xinqidev.pattern.strategy.userservice;
import com.xinqi.xinqidev.entity.enumeration.HelloEnum;
import com.xinqi.xinqidev.pattern.strategy.HelloStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xin麒
 * @date 2023/10/21 0:10
 * //这里体现了工厂模式
 */
@Component
public class HelloStrategyUseService implements ApplicationContextAware {
    private Map<HelloEnum, HelloStrategy> helloStrategyMap = new ConcurrentHashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, HelloStrategy> tmpMap = applicationContext.getBeansOfType(HelloStrategy.class);
        System.out.println("tmpMap is "+tmpMap);
        tmpMap.values().forEach(helloStrategy -> helloStrategyMap.put(helloStrategy.gainSettleStatusEnum(),helloStrategy));
    }

    public Integer processDetail(HelloEnum helloEnum,String argument1, String argument2, List<Object> list){
        HelloStrategy strategy = helloStrategyMap.get(helloEnum);//标识符
        if (strategy == null) return null;
        return strategy.processDetail(argument1,argument2,list);
    }

}
